package com.qianniu.napi.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.Goods;
import com.qianniu.napi.admin.entity.Store;
import com.qianniu.napi.admin.mapper.GoodsMapper;
import com.qianniu.napi.admin.service.IGoodsService;
import com.qianniu.napi.admin.web.GoodsController;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品信息 服务实现类
 * </p>
 *
 * @author chenxuebin
 * @since 2018-06-23
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    private static final Logger logger = LoggerFactory.getLogger(GoodsServiceImpl.class);

    @Autowired
    private GoodsMapper goodsMapper;

    public List<Goods> getGoodsByMap(Map map) {
        return goodsMapper.getBaseGoodsByMap(map);
    }

    public List<Goods> getYxgoodsList(int pageIndex,int pageSize,Double lat,Double lng,Long mid,Long mpid,String hytype,String yxtype,String shicode,String orderby) {
        List<Goods> list = null;
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("mid", mid);
        map.put("mpid", mpid);
        map.put("shicode", shicode);
        if (!Strings.isNullOrEmpty(yxtype)) map.put("yxtype", yxtype);
        if (!Strings.isNullOrEmpty(hytype)) map.put("hytype", hytype);

        int total = goodsMapper.getYxgoodsCount(map);

        if (total == 0) return null;
        int skip = (pageIndex - 1) * pageSize;
        if (Strings.isNullOrEmpty(orderby)) orderby = "distance";

        logger.debug("==========getYxgoodsList 111");

        map.put("lat", lat);
        map.put("lng", lng);
        map.put("skip", skip);
        map.put("size", pageSize);
        map.put("orderby", orderby);
        String sort = "desc";
        if (!Strings.isNullOrEmpty(orderby) && !orderby.equals("distance")) {
            sort = "asc";
        }
        map.put("sort", sort);
        logger.debug("==========getYxgoodsList map="+JSON.toJSONString(map));
        list = goodsMapper.getYxgoodsList(map);

        return list;
    }
}
